package pl.un4giv3n.mightvsmagic.action;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.servlet.ServletContext;

import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import pl.un4giv3n.mightvsmagic.entity.User;
import pl.un4giv3n.mightvsmagic.hibernate.HibernateFilter;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author andrzejewskia
 * main action providing base fields and methods
 */
public class PersistenceAction extends ActionSupport implements
		ServletContextAware, SessionAware {

	private static final long serialVersionUID = 1L;
	protected static final String ACTION_START = "actionStart";
	protected static final String ADMIN_PAGE = "adminPage";
	protected static final String ACTION_END = "actionEnd";
	protected static final long ONE_WEEK = 7 * 24 * 3600 * 1000;
	protected static final long ONE_HOUR = 3600 * 1000;
	private ServletContext context;
	private Map<String, Object> session;

	protected static final String USER_ID = "sessionUser";

	@Override
	public String execute() {
		return SUCCESS;
	}

	protected EntityManager getEntityManager() {
		EntityManagerFactory emf = (EntityManagerFactory) context
				.getAttribute(HibernateFilter.ENTITY_MANAGER_FACTORY);
		return emf.createEntityManager();
	}

	@Override
	public void setServletContext(ServletContext context) {
		this.context = context;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	protected Map<String, Object> getSession() {
		return session;
	}

	protected Long getSessionUserId() {
		return (Long) session.get(USER_ID);
	}
	
	protected User getSessionUser(EntityManager em) {
		Long sessionUserId = getSessionUserId();
		if(sessionUserId == null){
			return null;
		}
		return em.find(User.class, sessionUserId);
	}

	protected ServletContext getContext() {
		return context;
	}
	
	@SuppressWarnings("unchecked")
	public int getUnreadMessagesCount(){
		int count = 0;
		EntityManager entityManager = getEntityManager();
		Query query = entityManager .createNamedQuery("find.recent.unread.messages.by.reciverId");
		query.setParameter("reciverId", getSessionUserId());
		query.setParameter("sendTime", System.currentTimeMillis()-ONE_WEEK);
		List resultList = query.getResultList();
		if(resultList != null){
			 count = resultList.size();
		}
		entityManager.close();
		return count;
	}
	
	@SuppressWarnings("unchecked")
	public int getUnreadDefensesCount() {
		int unreadDefenses = 0;
		EntityManager entityManager = getEntityManager();
		Query query = entityManager .createNamedQuery("find.user.unread.defenses");
		query.setParameter("id", getSessionUserId());
		query.setParameter("time", System.currentTimeMillis() - ONE_WEEK);
		List resultList = query.getResultList();
		if(resultList != null){
			unreadDefenses = resultList.size();
		}
		entityManager.close();
		return unreadDefenses ;
	}
	
	/**
	 * this is required for hiding countdown clock on error page
	 * if you want to show the clock, override and return formatted action end time
	 * @return empty string
	 */
	public String getActionEndString(){
		return "";
	}
	
}
